<template>
    <div class="app-container">


        <el-tabs type="border-card">
            <el-tab-pane label="亲人信息栏">




                <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="90px">
                    <el-form-item label="亲属姓名" prop="relativeName">
                        <el-input v-model="queryParams.relativeName" placeholder="请输入亲属姓名" clearable
                            @keyup.enter="handleQuery" />
                    </el-form-item>
                    <el-form-item label="是否在国外" prop="isAbroad">
                        <el-radio-group v-model="queryParams.isAbroad" @change="handleQuery">
                            <el-radio label="是">是</el-radio>
                            <el-radio label="否">否</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="证件号" prop="idNumber">
                        <el-input v-model="queryParams.idNumber" placeholder="请输入证件号" clearable
                            @keyup.enter="handleQuery" />
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                    </el-form-item>
                </el-form>


                <el-row :gutter="10" class="mb8">
                    <el-col :span="1.5">
                        <el-button type="primary" plain icon="Plus" @click="handleAdd"
                            v-hasPermi="['personal:relatives:add']">新增</el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
                            v-hasPermi="['personal:relatives:edit']">修改</el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
                            v-hasPermi="['personal:relatives:remove']">删除</el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button type="warning" plain icon="Download" @click="handleExport"
                            v-hasPermi="['personal:relatives:export']">导出</el-button>
                    </el-col>
                    <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
                </el-row>

                <el-table v-loading="loading" :data="relativesList" @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="55" align="center" />

                    <el-table-column label="姓名" align="center" prop="relativeName" width="70" />
                    <el-table-column label="本人关系" align="center" prop="relation" width="80" />
                    <el-table-column label="共同生活" align="center" prop="isLiveChildren" width="80" />
                    <el-table-column label="婚姻状况" align="center" prop="maritalStatus" width="80" />

                    <el-table-column label="工作(学习)单位" align="center" prop="workStudyUnit" />
                    <el-table-column label="现任职务" align="center" prop="position" width="80" />
                    <el-table-column label="单位性质" align="center" prop="unitNature" width="80" />
                    <el-table-column label="担任高级职务" align="center" prop="isSeniorPosition" width="110" />
                    <el-table-column label="是否在国外" align="center" prop="isAbroad" />
                    <el-table-column label="证件名称" align="center" prop="certificateName" width="80" />
                    <el-table-column label="证件号" align="center" prop="idNumber" width="100" />
                    <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                        <template #default="scope">
                            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                                v-hasPermi="['personal:relatives:edit']">修改</el-button>
                            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                                v-hasPermi="['personal:relatives:remove']">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>

                <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
                    v-model:limit="queryParams.pageSize" @pagination="getList" />



            </el-tab-pane>
            <el-tab-pane label="亲人出国栏">
                <Abroad></Abroad>
            </el-tab-pane>
            <el-tab-pane label="刑事责任栏">
                <Responsibility></Responsibility>
            </el-tab-pane>
            <el-tab-pane label="亲人移居栏">
                <Immigration></Immigration>
            </el-tab-pane>
        </el-tabs>




        <!-- 添加或修改直属亲人，用于记录直属亲人的相关信息对话框 -->
        <el-dialog :title="title" v-model="open" width="500px" append-to-body>
            <el-form ref="relativesRef" :model="form" :rules="rules" label-width="150px">
                <el-form-item label="亲属姓名" prop="relativeName">
                    <el-input v-model="form.relativeName" placeholder="请输入亲属姓名" />
                </el-form-item>
                <!-- <el-form-item label="证件名称" prop="certificateName">
                    <el-input v-model="form.certificateName" placeholder="请输入证件名称" />
                </el-form-item> -->

                <el-form-item label="证件名称" prop="certificateName">
                    <el-select v-model="form.certificateName" placeholder="请选择证件名称">
                        <el-option v-for="item in certificateOptions" :key="item.value" :label="item.label"
                            :value="item.label">
                        </el-option>
                    </el-select>
                </el-form-item>





                <el-form-item label="证件号" prop="idNumber">
                    <el-input v-model="form.idNumber" placeholder="请输入证件号" />
                </el-form-item>
                <el-form-item label="是否与子女共同生活" prop="isLiveChildren">
                    <el-radio-group v-model="form.isLiveChildren">
                        <el-radio label="是">是</el-radio>
                        <el-radio label="否">否</el-radio>
                    </el-radio-group>

                </el-form-item>
                <el-form-item label="婚姻变化情况" prop="maritalChange">
                    <el-input v-model="form.maritalChange" placeholder="请输入婚姻变化情况" />
                </el-form-item>
                <el-form-item label="工作(学习)单位" prop="workStudyUnit">
                    <el-input v-model="form.workStudyUnit" placeholder="请输入工作(学习)单位" />
                </el-form-item>
                <el-form-item label="现任职务" prop="position">
                    <el-input v-model="form.position" placeholder="请输入现任职务" />
                </el-form-item>



                <el-form-item label="单位性质" prop="unitNature">
                    <el-select v-model="form.unitNature" placeholder="请选择单位性质">
                        <el-option v-for="item in unitNatureOptions" :key="item.value" :label="item.label"
                            :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>



                <el-form-item label="是否担任高级职务" prop="isSeniorPosition">

                    <el-radio-group v-model="form.isSeniorPosition">
                        <el-radio label="是">是</el-radio>
                        <el-radio label="否">否</el-radio>
                    </el-radio-group>
                </el-form-item>
                <!-- <el-form-item label="是否在国" prop="isAbroad">
                    <el-input v-model="form.isAbroad" placeholder="请输入是否在国" />
                </el-form-item> -->


                <el-form-item label="是否在国外" prop="isAbroad">
                    <el-radio-group v-model="form.isAbroad">
                        <el-radio label="是">是</el-radio>
                        <el-radio label="否">否</el-radio>
                    </el-radio-group>
                </el-form-item>



            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup name="Relatives">
import Abroad from "../abroad/index.vue"
import Immigration from "../immigration/index.vue"
import Responsibility from "../responsibility/index.vue"
import { listRelatives, getRelatives, delRelatives, addRelatives, updateRelatives } from "@/api/personal/relatives";

const { proxy } = getCurrentInstance();

const relativesList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
    form: {},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        relativeName: null,
        relation: null,
        isLiveChildren: null,
        maritalStatus: null,
        maritalChange: null,
        workStudyUnit: null,
        position: null,
        unitNature: null,
        isSeniorPosition: null,
        isAbroad: null,
        certificateName: null,
        idNumber: null
    },
    rules: {
        relativeName: [
            { required: true, message: "亲属姓名不能为空", trigger: "blur" }
        ],
        relation: [
            { required: true, message: "与本人关系不能为空", trigger: "change" }
        ],
        isLiveChildren: [
            { required: true, message: "是否与子女共同生活不能为空", trigger: "blur" }
        ],
        isAbroad: [
            { required: true, message: "是否在国不能为空", trigger: "blur" }
        ],
        certificateName: [
            { required: true, message: "证件名称不能为空", trigger: "blur" }
        ],
        idNumber: [
            { required: true, message: "证件号不能为空", trigger: "blur" }
        ]
    }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询直属亲人，用于记录直属亲人的相关信息列表 */
function getList() {
    loading.value = true;
    listRelatives(queryParams.value).then(response => {
        relativesList.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
}

// 取消按钮
function cancel() {
    open.value = false;
    reset();
}

// 表单重置
function reset() {
    form.value = {
        id: null,
        relativeName: null,
        relation: null,
        isLiveChildren: null,
        maritalStatus: null,
        maritalChange: null,
        workStudyUnit: null,
        position: null,
        unitNature: null,
        isSeniorPosition: null,
        isAbroad: null,
        certificateName: null,
        idNumber: null
    };
    proxy.resetForm("relativesRef");
}

/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
}

/** 重置按钮操作 */
function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.id);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
    reset();
    open.value = true;
    title.value = "添加直属亲人，用于记录直属亲人的相关信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
    reset();
    const _id = row.id || ids.value
    getRelatives(_id).then(response => {
        form.value = response.data;
        open.value = true;
        title.value = "修改直属亲人，用于记录直属亲人的相关信息";
    });
}

/** 提交按钮 */
function submitForm() {
    proxy.$refs["relativesRef"].validate(valid => {
        if (valid) {
            if (form.value.id != null) {
                updateRelatives(form.value).then(response => {
                    proxy.$modal.msgSuccess("修改成功");
                    open.value = false;
                    getList();
                });
            } else {
                addRelatives(form.value).then(response => {
                    proxy.$modal.msgSuccess("新增成功");
                    open.value = false;
                    getList();
                });
            }
        }
    });
}

/** 删除按钮操作 */
function handleDelete(row) {
    const _ids = row.id || ids.value;
    proxy.$modal.confirm('是否确认删除直属亲人，用于记录直属亲人的相关信息编号为"' + _ids + '"的数据项？').then(function () {
        return delRelatives(_ids);
    }).then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
    }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
    proxy.download('personal/relatives/export', {
        ...queryParams.value
    }, `relatives_${new Date().getTime()}.xlsx`)
}



//证件类型列表
const certificateOptions = [
    { value: 'idCard', label: '身份证' },
    { value: 'passport', label: '护照' },
    { value: 'driverLicense', label: '驾驶证' },
    // 可以添加更多选项  
];
// 单位性质选项，这里直接使用了你的约束中的值  
const unitNatureOptions = [
    { value: '党政机关', label: '党政机关' },
    { value: '事业单位', label: '事业单位' },
    { value: '国有企业', label: '国有企业' },
    { value: '军队', label: '军队' },
    { value: '学生', label: '学生' },
    { value: '其他', label: '其他' },
    { value: '个体工商户', label: '个体工商户' },
    { value: '私营企业', label: '私营企业' },
    { value: '外商投资企业', label: '外商投资企业' },
    { value: '境内从事生产经营活动的外国（地区）企业', label: '境内从事生产经营活动的外国（地区）企业' },
    { value: '外国（地区）企业常驻竟内代表机构', label: '外国（地区）企业常驻竟内代表机构' },
    { value: '境外非政府组织境外内代表机构', label: '境外非政府组织境外内代表机构' }
];
getList();
</script>
